﻿using CompanyManageSystem.DLL;
using CompanyManageSystem.Model;
using EntityFramework.Extensions;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CompanyManageSystem.Service
{
    public class RepastService
    {
        PSContext _db;
        public RepastService()
        {
            _db = new PSContext();
        }

        #region 物料
        /// <summary>
        /// 获取所有物料
        /// </summary>
        /// <returns></returns>
        public List<Material> GetAllMaterial()
        {
            return _db.Materials.OrderBy(p => p.Code).ToList();
        }
        #endregion

        #region 单位
        /// <summary>
        /// 获取所有单位
        /// </summary>
        /// <returns></returns>
        public List<Unit> GetAllUnit()
        {
            return _db.Units.OrderBy(p => p.CreateTime).ToList();
        }
        #endregion

        #region 饭堂
        public List<Canteen> GetAllCanteen()
        {
            return _db.Canteens.OrderBy(p => p.Code).ToList();
        }
        #endregion

        #region 档口
        public List<Stall> GetAllStall()
        {
            return _db.Stalls.OrderBy(p => p.Code).ToList();
        }
        #endregion

        #region 供应商
        /// <summary>
        /// 获取所有供应商分类
        /// </summary>
        /// <returns></returns>
        public List<BaseElement> GetAllSupplierCategory()
        {
            return _db.SuppliersCategorys.ToList();
        }

        /// <summary>
        /// 获取所有供应商
        /// </summary>
        /// <returns></returns>
        public List<Supplier> GetAllSuppliers()
        {
            return _db.Suppliers.ToList();
        }

        /// <summary>
        /// 根据分类ID获取供应商
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Supplier> GetSupplierByCategoryID(Guid id)
        {
            return _db.Suppliers.Where(p => p.CategoryID == id).OrderBy(p => p.EnterTime).ToList();
        }

        /// <summary>
        /// 根据供应商ID获取供应商
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Supplier GetSupplierByID(Guid id)
        {
            return _db.Suppliers.Where(p => p.ID == id).FirstOrDefault();
        }

        /// <summary>
        /// 添加供应商
        /// </summary>
        /// <param name="categoryID"></param>
        /// <param name="code"></param>
        /// <param name="name"></param>
        /// <param name="contracts"></param>
        /// <param name="phone"></param>
        /// <param name="address"></param>
        /// <returns></returns>
        public Supplier AddSupplier(Guid categoryID, string code, string name, string contracts, string phone, string address)
        {
            Supplier supplier = new Supplier
            {
                ID = Guid.NewGuid(),
                Code = code,
                Name = name,
                Contracts = contracts,
                Phone = phone,
                Address = address,
                EnterTime = DateTime.Now,
                CategoryID = categoryID,
                IsChecked = false,
            };
            _db.Suppliers.Add(supplier);
            _db.SaveChanges();
            return supplier;
        }

        /// <summary>
        /// 编辑供应商
        /// </summary>
        /// <param name="id"></param>
        /// <param name="code"></param>
        /// <param name="name"></param>
        /// <param name="address"></param>
        /// <param name="contracts"></param>
        /// <param name="phone"></param>
        /// <returns></returns>
        public Supplier UpdateSupplier(Guid id, string code, string name, string contracts, string phone, string address)
        {
            var supplier = _db.Suppliers.Where(p => p.ID == id).FirstOrDefault();
            supplier.Code = code;
            supplier.Name = name;
            supplier.Address = address;
            supplier.Contracts = contracts;
            supplier.Phone = phone;
            _db.SaveChanges();
            return supplier;
        }

        /// <summary>
        /// 根据勾选的CheckBox和分类ID删除供应商
        /// </summary>
        /// <param name="categoryID"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelSupplierByCheckedID(Guid categoryID, Guid[] id)
        {
            return _db.Suppliers.Where(p => p.CategoryID == categoryID && id.Contains(p.ID)).Delete();
        }
        #endregion

        #region 采购入库
        /// <summary>
        /// 获取当天所有采购订货单明细
        /// </summary>
        /// <returns></returns>
        public List<PurchaseOrderDetail> GetAllOrderDetailOnTheDay()
        {
            var date = DateTime.Now.Date;
            return _db.PurchaseOrderDetails.Where(p => DbFunctions.TruncateTime(p.CreateTime) == date).OrderBy(p => p.CreateTime).ToList();
        }

        /// <summary>
        /// 根据各种条件获取采购订货明细
        /// </summary>
        /// <param name="name"></param>
        /// <param name="code"></param>
        /// <param name="stallId"></param>
        /// <param name="canteenId"></param>
        /// <returns></returns>
        public List<PurchaseOrderDetail> GetOrderDetailByFactor(string name,string code,Guid? stallId,Guid? canteenId)
        {
            var date = DateTime.Now.Date;
            var canteen = "";
            var stall = "";

            if (canteenId != null)
            {
                canteen = _db.Canteens.Find(canteenId).Name;
            }
            if (stallId != null)
            {
                stall = _db.Stalls.Find(stallId).Name;
            }
            
            var data = _db.PurchaseOrderDetails
                .Join(_db.Materials, p => p.MaterialID, p => p.ID, (a, b) => new { a, b })
                .WhereIf(!string.IsNullOrEmpty(stall), p => p.a.Stall == stall)
                .WhereIf(!string.IsNullOrEmpty(canteen), p => p.a.Canteen == canteen)
                .Where(p => DbFunctions.TruncateTime(p.a.CreateTime) == date)
                .Where(p => p.a.MaterialName.Contains(name))
                .Where(p => p.b.Code.Contains(code))
                .Select(p => p.a)
                .ToList();
            return data;
        }

        /// <summary>
        /// 获取所有采购入库主单
        /// </summary>
        /// <returns></returns>
        public List<PurchaseStorageOrder> GetAllStorageOrder()
        {
            return _db.PurchaseStorageOrders.OrderBy(p => p.CreateTime).ToList();
        }

        /// <summary>
        /// 添加新的采购入库明细
        /// </summary>
        /// <param name="list"></param>
        public void AddStorageOrder(List<PurchaseStorageOrderDetail> list,Guid headerId)
        {
            foreach (var item in list)
            {
                item.ID = Guid.NewGuid();
                item.CreateTime = DateTime.Now;
                item.PurchaseStorageOrderID = headerId;
                _db.PurchaseStorageOrderDetails.Add(item);
            }
             _db.SaveChanges();
        }
        #endregion


    }
}
